package org.csu.lestore.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Base64;

@Controller
@RequestMapping("/img/")
public class ImgController {//现在转为使用util获取图片的路径
    @GetMapping("getImgSrc")
    public void getImgSrc(HttpServletRequest request, HttpServletResponse response,@RequestParam String name, @RequestParam String type)  {
        String filePath=System.getProperty("user.dir")+"\\src\\main\\resources\\static\\images\\";
        if (type != null && type.equals("user"))
            filePath +="user\\";
        else if(type != null && type.equals("product"))
            filePath += "product\\";
        else if(type != null && type.equals("category"))
            filePath += "category\\";
        else if(type != null && type.equals("item"))
            filePath += "item\\";
        else
            filePath += "else\\";
        String fileName =filePath+name;
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            byte[] data = null;
            data = new byte[in.available()];
            in.read(data);
            in.close();
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(data);
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}









